<template>
    <div class="home">
        <!-- 顶部区域布局 -->
        <div class="header">
            <div class="item">
                总销售额
                <div class="num">
                    {{totalData.saleTotal}}
                </div>
                <div class="bottom">
                    今日销售额: {{totalData.sale}}
                </div>
            </div>
            <div class="item">
                总访问量
                <div class="num">
                    {{totalData.views}}
                </div>
                <div class="bottom">
                    今日访问量: {{totalData.viewsTotal}}
                </div>
            </div>
            <div class="item">
                总收藏量
                <div class="num">
                    {{totalData.collect}}
                </div>
                <div class="bottom">
                    今日收藏量: {{totalData.collectTotal}}
                </div>
            </div>
            <div class="item">
                总支付量
                <div class="num">
                    {{totalData.pay}}
                </div>
                <div class="bottom">
                    今日支付量: {{totalData.payTotal}}
                </div>
            </div>
        </div>
        <!-- 访问统计 -->
        <div class="content">
            <div class="time-info" id="box13">
                <div class="title">
                    月销售额
                </div>
                <div id="charts" style="width: 100%; height: 300px;" ></div>
            </div>
            <div class="area" id="box14">
                <div class="title">
                    产品销售比例
                </div>
                <div id="pie" style="width: 100%; height: 300px;">

                </div>
            </div>
        </div>
        <!-- 最新内容 -->
        <div class="home-footer">
            <el-card class="box-card">
                <div class="clearfix">
                    <span>今日订单</span>
                </div>
                <div class="text item">
                    <el-row>
                        <el-col :span="8">
                            <div class="title">今日订单量:</div>
                            <div>{{orderData.orderCount}}</div>
                        </el-col>
                        <el-col :span="8">
                            <div class="title">汇总确认订单:</div>
                            <div>{{orderData.curOrderCount}}</div>
                        </el-col>
                        <el-col :span="8">
                            <div class="title">累计金额:</div>
                            <div>{{orderData.money}}</div>
                        </el-col>
                    </el-row>
                </div>
            </el-card>
            <el-card class="box-card">
                <div class="clearfix">
                    <span>本月订单</span>
                </div>
                <div class="text item">
                    <el-row>
                        <el-col :span="8">
                            <div class="title">本月订单量:</div>
                            <div>{{orderData.orderCount}}</div>
                        </el-col>
                        <el-col :span="8">
                            <div class="title">汇总确认订单:</div>
                            <div>{{orderData.orderCount}}</div>
                        </el-col>
                        <el-col :span="8">
                            <div class="title">累计金额:</div>
                            <div>{{orderData.money}}</div>
                        </el-col>
                    </el-row>
                </div>
            </el-card>
            <el-card class="box-card">
                <div class="clearfix">
                    <span>快捷入口</span>
                </div>
                <div class="text item">
                    <el-row>
                        <el-col :span="8">
                            <el-button type="primary">产品管理</el-button>
                        </el-col>
                        <el-col :span="8">
                            <el-button type="success">消息管理</el-button>
                        </el-col>
                        <el-col :span="8">
                            <el-button type="danger">内容管理</el-button>
                        </el-col>
                       
                    </el-row>
                    
                </div>
            </el-card>
        </div>
    </div>
</template>
<script setup>
    // 导入定义的home接口
    import { getHomeTotal,  getFormat, getOrderInfo } from '@/api/home'
    import echarts from '@/plugins/echarts'
    import { onMounted, ref } from 'vue';
    const totalData = ref({});
    const orderData = ref({});
    console.log('echarts-->', echarts);
    const getIndexHomeTotal = async () => {
        let res = await getHomeTotal();
        totalData.value = res.data.data.list;
        console.log('首页数据', totalData);
    }
    // 获取订单数据
    const getIndexOrderInfo = async () => {
        let res = await getOrderInfo();
        orderData.value = res.data.list;
        console.log('获取首页订单数据', orderData);
    }
    // 获取统计图表数据
    const getIndexFormat = async () => {
        let res = await getFormat();
        let data = res.data.list;
        console.log('首页图表数据', data);
        let xAxis = [];
        let lineData = [];
        let barData = [];
        let pieData = [];
        data.forEach(ele => {
            xAxis.push(ele.name);
            lineData.push(ele.total_num);
            barData.push(ele.real_num);
            pieData.push({"name":ele.name, "value": ele.real_num});
        });
        drawTable(xAxis, lineData, barData);
        drawPie(pieData);
    }

    const drawTable = (xAxis, lineData, barData) => {
        // 解决一个echarts不渲染的问题，认为dom没发修改
        let disable_dom = document.getElementById('charts');
        disable_dom.removeAttribute("_echarts_instance_");
        var myChart = echarts.init(disable_dom);
        // let myChart = echarts.init(document.getElementById('charts'));
        // 绘制图表
        myChart.setOption({
            tooltip: {},
            xAxis: {
                data: xAxis
            },
            yAxis: {},
            series: [
                {
                    name: '销量',
                    type: 'line',
                    data: lineData
                },
                {
                    name: '销量额',
                    type: 'bar',
                    data: barData
                }
            ]
        });
    }
    const drawPie = (pieData) => {
        // let myChart = echarts.init(document.getElementById('pie'));
        // 解决一个echarts不渲染的问题，认为dom没发修改
        let disable_dom = document.getElementById('pie');
        disable_dom.removeAttribute("_echarts_instance_");
        var myChart = echarts.init(disable_dom);
        // 绘制图表
        let option = {
            title: {
                // text: 'Referer of a Website',
                // subtext: 'Fake Data',
                // left: 'center'
            },
            tooltip: {
                trigger: 'item'
            },
            legend: {
                orient: 'vertical',
                left: 'left'
            },
            series: [
                {
                name: '销售额',
                type: 'pie',
                radius: '50%',
                data: pieData,
                emphasis: {
                    itemStyle: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
                }
            ]
        };
        myChart.setOption(option);
    }
    // 进入界面就需要获取数据--生命周期函数
    onMounted( () => {
        getIndexHomeTotal();
        getIndexOrderInfo();
        getIndexFormat();
       
    });

    // 数据格式, vue3没有过滤器 --- 123456 --> 123,456
    const toLocaleString = (value) => {
        if (!value) return;
        return value.toLocaleString();
    }


</script>
<style scoped>
    .header {
        display: flex;
        color: #fff;
        .item {
            flex: 1;
            background: #fff;
            margin: 0 20px;
            border-radius: 10px;
            height: 100px;
            font-weight: bold;
            padding: 10px 5px;
            .num {
                font-size: 22px;
                margin: 10px;
            }
            .bottom {
                padding: 10px 20px;
                border-top: 1px solid rgb(246, 245, 245);
                font-weight: normal;
            }
        }
        .item:nth-child(1) {
            background-image: linear-gradient(#df887d, #88554d);
        }
        .item:nth-child(2) {
            background-image: linear-gradient(#409eff, #2e556e);
        }
        .item:nth-child(3) {
            background-image: linear-gradient(#b54fa8, #713c7a);
        }
        .item:nth-child(4) {
            background-image: linear-gradient(#1cd2f1, #39717a);
        }
    }
    .content {
        margin: 20px;
        display: flex;
        .time-info {
            flex: 2;
            background: #fff;
            margin-right: 20px;
            padding: 10px;
        }
        .area{
            flex: 1;
            background: #fff;
            padding: 10px;
        }
    }
    .home-footer {
        display: flex;
        padding-left: 20px;
        .box-card {
            flex: 1;
            margin-right: 20px;
            span {
                font-weight: 600;
            }
            .item {
                text-align: center;
                font-size: 24px;
                .el-col {
                    border-right: 1px solid #eee;
                }
                .el-col:last-child {
                    border-right: none;
                }
                .title {
                    margin-bottom: 10px;
                    font-size: 14px;
                }
            }
            .clearfix{
                margin-bottom: 20px;
            }
        }
    }
</style>
